<?php

/**
 * 前台访问控制器类
 * 
 * @package    01CMS
 * @subpackage www
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class DataController extends Controller
{
    public $keyWord;
    public $templateDir;
    
    function __construct()
    {
        parent::__construct();
        $this->templateDir = ROOT_PATH . '/template/' . $this->Load->var['style'];
    }

    function test ()
    {
        //
    }

    function index ()
    {
        model("Html")->indexHtml();
    }

    function category ($id = 0, $start = 0)
    {
        model("Html")->showCategoryHtml($id, $start);
    }

    function archive ($id = 0, $start = 0)
    {
        model("Html")->showArchiveHtml($id, $start);
    }

    function search ()
    {
        $query = $this->Load->query('k');
        $k = $query['k'];
        $k = trim($k);
        
        if (strlen($k) < 3)
        {
            showInfo('关键词不能少于3字节或2个汉字');
        }
        
        $this->keyWord = $k;
        $this->table = '@@__archive_archive';
        $this->where = "`title` like '%{$k}%' or `desc` like '%{$k}%'";
        $this->dealListsControllerMethod = 'highLight';
        $this->baseUrl = BOOT_URL . '/search';
        $this->action('lists');
    }

    function highLight ($lists = '')
    {
        if (is_array($lists))
        {
            $count = count($lists);
            for ($i = 0; $i < $count; $i ++)
            {
                $lists[$i]->title = str_replace($this->keyWord, '<span>' . $this->keyWord . '</span>', $lists[$i]->title);
                $lists[$i]->desc = str_replace($this->keyWord, '<span>' . $this->keyWord . '</span>', $lists[$i]->desc);
            }
            return $lists;
        }
        return FALSE;
    }
}